import android.widget.TextView;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

/**
 * 运行步骤：
 * 1.生成build.xml
 * 	 android create uitest-project -n UiTestCase -t 4 -p "D:\ANDROID\workspace\UiTestCase"
 * 2.使用Eclipse的Ant Build
 * 	1)配置：Project-> Properties-> Builder -> Build file-> Build Directory
 * 	2)修改build.xml中default的help值为build
 * 	3)右键run as-> ant Build
 * 3.生成了UiTestCase.jar
 * 4.push到机器中
 *   adb push UiTestCase.jar /data/local/tmp
 * 5.运行UiAutomator
 *   adb shell uiautomator runtest UiTestCase.jar -c UiTestCase
 *
 */

//===========================Monkey压力测试
/*	adb shell monkey -p com.zhonghong.zhmedia -v 5000
 		-s <seed>
		伪随机数生成器的seed值。如果用相同的seed值再次运行Monkey，它将生成相同的事件序列。

		--throttle <milliseconds>
		在事件之间插入固定延迟。通过这个选项可以减缓Monkey的执行速度。如果不指定该选项，Monkey将不会被延迟，事件将尽可能快地被产成。
*/
public class UiTestCase extends UiAutomatorTestCase {

	public void testCase1() throws UiObjectNotFoundException {
		// 1.启动APP
		getUiDevice().pressHome();
		UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
		allAppsButton.clickAndWaitForNewWindow();

		UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
		appsTab.click();

		UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));

		UiObject settingsApp = appViews.getChildByText(new UiSelector().className(TextView.class.getName()), "Efilm");
		settingsApp.clickAndWaitForNewWindow();

		// 2.进入主界面
		UiObject tv1 = new UiObject(new UiSelector().text("影院"));
		tv1.click();

		// 3.点击影院
		UiObject cinema = new UiObject(new UiSelector().description("cinema_row"));
		cinema.waitForExists(60000);
		cinema.clickAndWaitForNewWindow();

		// 4.场次
		UiObject occ = new UiObject(new UiSelector().description("LinearLayout10"));
		cinema.waitForExists(60000);
		occ.clickAndWaitForNewWindow();

		// 5.座位
		UiObject oseat = new UiObject(new UiSelector().description("cinema_shows_list_item").index(0).childSelector(
			new UiSelector().description("LinearLayout10")));
		oseat.waitForExists(5000);
		int h = getUiDevice().getDisplayHeight();
		int w = getUiDevice().getDisplayWidth();
		getUiDevice().click(h / 2, w / 2);

		// ====================截座位图================
		Process process;
		try {
			process = Runtime.getRuntime().exec("screencap /mnt/sdcard/EfilmFailSnapShot01.png");
			process.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 进入短信界面，编辑一条Hello World
	public void testCase2() throws UiObjectNotFoundException {
		UiDevice device = getUiDevice();
		device.pressHome();

		UiObject appsTab = new UiObject(new UiSelector().description("Apps"));
		appsTab.click();

		// 因为短信界面在第二个列表页面，所以我们可以先滑动一下
		// 通过scrollable属性来选定滑动view
		UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
		appViews.setAsHorizontalList();
		appViews.scrollForward();

		// 通过类名和Text找到短信应用图标，Text获取通过uiautomatorviewer
		UiObject messageApp = appViews.getChildByText(new UiSelector().className(android.widget.TextView.class
			.getName()), "Messaging");
		// 点击并等待打开短信应用
		messageApp.clickAndWaitForNewWindow();

		// 选择新建短信
		UiObject newMessage = new UiObject(new UiSelector().description("New message"));
		newMessage.click();

		// 选择编辑信息
		UiObject editMessage = new UiObject(new UiSelector().text("Type text message"));
		editMessage.click();
		editMessage.setText("hello world!");
	}
}
